package link.ten_fei.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import link.ten_fei.Bean.Doc_User;
import link.ten_fei.Bean.Pl_List;
import link.ten_fei.service.QueryMRService;
import link.ten_fei.service.QueryMRServiceImpl;

/**
 * Servlet implementation class QueryMRServlet
 */
@WebServlet("/queryMR.action")
public class QueryMRServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QueryMRServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		// 获取修改用户的id
		String id = request.getParameter("id");
		Doc_User doc_User = (Doc_User) request.getSession().getAttribute("session_user");
		QueryMRService queryMRService = new QueryMRServiceImpl();
		if (action == null) {
			if (doc_User.getDoc_judge() == 0) {
				String pl_name = request.getParameter("pl_name");
				String flag = null;
				if (request.getParameter("name") == null) {
					flag = "病人名字";
				} else {
					flag = request.getParameter("name");
				}
				request.setAttribute("flag", flag);
				if (pl_name == null || pl_name.equals("")) {
					request.setAttribute("query_tip", "请输入...");
				} else if (pl_name != null && !pl_name.equals("")) {
					request.setAttribute("query_tip", pl_name);
				}
				// 查询所有病历,将查询到的对象封装在一个集合中
				List<Pl_List> lists = queryMRService.find(pl_name, flag, 0,0,doc_User.getDoc_judge());
				request.setAttribute("lists", lists);
				request.setAttribute("mainRight", "/WEB-INF/jsp/queryMR.jsp");
				request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
			} else {
				String pl_name = request.getParameter("pl_name");
				if (pl_name == null || pl_name.equals("")) {
					request.setAttribute("query_tip", "请输入病人姓名...");
				} else if (pl_name != null && !pl_name.equals("")) {
					request.setAttribute("query_tip", pl_name);
				}
				// 查询所有病历,将查询到的对象封装在一个集合中
				List<Pl_List> lists = queryMRService.find(pl_name, null, 0,doc_User.getId(),doc_User.getDoc_judge());
				request.setAttribute("lists", lists);
				request.setAttribute("mainRight", "/WEB-INF/jsp/queryMR.jsp");
				request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
			}
		} else if (action != null && action.equals("print")) {
			// 查询根据添加页面来写，可以重复使用添加方法
			// 根据id查询用户
			List<Pl_List> lists = queryMRService.find(null, null, Integer.parseInt(id),0,doc_User.getDoc_judge());
			Pl_List list = null;
			// 保存用户信息，用于在前端展示
			// lists为多个实体类的对象的地址
			// list为单个实体类的对象的地址
			for (int i = 0; i < lists.size(); i++) {
				list = lists.get(i);
			}
			request.setAttribute("list", list);
			// 跳转到修改页面
			request.getRequestDispatcher("/WEB-INF/jsp/print.jsp").forward(request, response);
		} else if (action != null && action.equals("modify")) {
			// 查询根据添加页面来写，可以重复使用添加方法
			// 根据id查询用户
			List<Pl_List> lists = queryMRService.find(null, null, Integer.parseInt(id),0,doc_User.getDoc_judge());
			Pl_List list = null;
			// 保存用户信息，用于在前端展示
			// lists为多个实体类的对象的地址
			// list为单个实体类的对象的地址
			for (int i = 0; i < lists.size(); i++) {
				list = lists.get(i);
			}
			request.setAttribute("list", list);
			// 跳转到修改页面
			request.setAttribute("mainRight", "/WEB-INF/jsp/addMR.jsp");
			request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
		} else if (action != null && action.equals("delete")) {
			int flag = queryMRService.deleteById(Integer.parseInt(id));
			PrintWriter writer = response.getWriter();
			if (flag == 1) {
				writer.print("删除成功！！！");
			} else if (flag == 0) {
				writer.print("已删除！！！");
			}
		}

	}

}
